﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Web.UI.WebControls;
using SchemaMonitor.json;
using Framework;
using SchemaMonitor;

/// <summary>
/// Summary description for CUsercontrolWithTable
/// </summary>
public class CUsercontrolWithTable : System.Web.UI.UserControl
{




	public TableHeaderCell IdCell2(TableRow tr, SchemaMonitor.CNode node)
	{
		var r = node.Root;
		var th = IdCell(tr, r);

		var lbl = new Label();
		var div = new Panel();
		div.Controls.Add(lbl);

		var tags = r.Tags;
		if (tags.Contains("created_time"))
		{
			string s = r.GetString("created_time");
			var d = SchemaMonitor.html.CHtml.ToDate(s);
			lbl.Text = CUtilities.Timespan(d);
		}
		else
			lbl.Text = CUtilities.Timespan(node.NodeCreated);

		var p = new CSoftPost(node.Root, CSession.User);

		var dd = new DropDownList();
		dd.DataTextField = "TagName";
		dd.DataValueField = "TagId";
		dd.DataSource = p.RemainingMetaTags();
		dd.DataBind();
		CDropdown.BlankItem(dd, "-- Tag --");
		dd.AutoPostBack = true;
		dd.SelectedIndexChanged += Dd_SelectedIndexChanged;
		dd.ID = "dd_" + node.Root.Id;

		th.Controls.Add(div);
		if (r.TypeId == CType.Cache.GetBySig("post").TypeId)
			th.Controls.Add(dd);

		return th;
	}

	private void Dd_SelectedIndexChanged(object sender, EventArgs e)
	{
		var dd = (DropDownList)sender;
		var id = dd.ID.Substring(dd.ID.IndexOf("dd_") + 3);
		var tagId = CDropdown.GetInt(dd);
		var guid = CNodeRoot.Str2Guid(id);
		//var node = CSession.User.All.GetById(guid);
		var nl = CNodeLabel.Cache.GetById(guid, tagId);
		if (null == nl)
		{
			nl = new CNodeLabel();
			nl.LabelNodeGuid = guid;
			nl.LabelTagId = tagId;
			nl.Save();
		}
		Response.Redirect(Request.RawUrl);
	}

	public TableHeaderCell IdCell(TableRow tr, CNodeRoot node)
	{
		var th = Th(tr, node.Id);
		string url = CSitemap.DataView(node);
		Link(th, url).ForeColor= System.Drawing.Color.LightBlue;
		return th;
	}


	public TableHeaderCell Th(TableRow tr, string s)
	{
		var t = new TableHeaderCell();
		tr.Cells.Add(t);
		t.Text = s;
		return t;
	}


	public TableCell Td(TableRow tr, SchemaMonitor.CNode i, string tag)
	{
		string s = string.Empty;
		var ff = i.Root.Fields;
		if (ff.Has(tag))
			s = i.Root.GetString(tag);
		return Td(tr, s);
	}
	public TableCell Td(TableRow tr, int count)
	{
		var td = Td(tr, count == 0 ? string.Empty : count.ToString());
		td.HorizontalAlign = HorizontalAlign.Right;
		return td;
	}
	public TableCell Td(TableRow tr, string s, bool big = false)
	{
		s = s ?? string.Empty;
		var td = new TableCell();
		tr.Cells.Add(td);
		td.Text = s;
		if (s.Length > 100)
		{
			var c = "bigtext";
			if (!big)
				c = "medtext";
			else
				tr.CssClass = "alt_row";

			s = HttpUtility.HtmlEncode(s);
			s = s.Replace("\r\n", "\n").Replace("\n", "<br>");
			td.Text = string.Concat("<div class='", c, "'>", s, "</div>");
		}
		return td;
	}
	public TableCell Td_Edit(TableRow tr, string s, string key, CFieldList f, bool savePending)
	{
		if (f[key].Tag.FieldType.TypeName.ToLower() != "string")
			return this.Td(tr, s);

		if (savePending)
		{
			s = Request.Form["txt" + key];
			if (f[key].Tag.FieldType.TypeName.ToLower() == "string")
				if (key != "id")
					f[key].String = s;
		}

		s = s ?? string.Empty;
		var td = new TableCell();
		tr.Cells.Add(td);

		var txt = new TextBox();
		td.Controls.Add(txt);
		txt.Text = s;
		txt.ID = "txt" + key;
		txt.Width = new Unit("800");
		if (s.Length > 100)
		{
			txt.TextMode = TextBoxMode.MultiLine;
			txt.Height = new Unit("600px");
		}
		tr.CssClass = "alt_row";
		return td;
	}

	public HyperLink Link(TableCell td, string url)
	{
		var lnk = new HyperLink();
		lnk.Text = td.Text;
		lnk.ToolTip = td.ToolTip;
		lnk.NavigateUrl = url;
		td.Text = string.Empty;
		td.Controls.Add(lnk);
		return lnk;
	}


	public TableCell ImgWithLink(TableRow tr, string id)
	{
		return ImgWithLink(tr, long.Parse(id));
	}
	public TableCell ImgWithLink(TableRow tr, long id)
	{
		var td = new TableCell();
		ImgWithLink(td, id);
		tr.Cells.Add(td);
		return td;
	}
	public TableCell ImgWithLink(TableCell td, string id)
	{
		return ImgWithLink(td, long.Parse(id));
	}
	public TableCell ImgWithLink(TableCell td, long id)
	{
		td.Controls.Add(ImgWithLink(id));
		return td;
	}
	public HyperLink ImgWithLink(string id)
	{
		return ImgWithLink(long.Parse(id));
	}
	public HyperLink ImgWithLink(long id)
	{
		var img = Img(id);

		var lnk = new HyperLink();
		lnk.Target = "_blank";
		lnk.NavigateUrl = img.ImageUrl;
		lnk.Controls.Add(img);
		return lnk;
	}
	public Image Img(string id, int width = 120)
	{
		return Img(long.Parse(id), width);
	}
	public Image Img(long id, int width = 120)
	{
		var img = new Image();
		img.Width = new Unit(width);
		img.ImageUrl = CSitemap.Photo(id);
		img.AlternateText = id.ToString();
		return img;
	}
	public TableCell RawImg(TableRow tr, string url, int height = 50)
	{
		var td = new TableCell();
		tr.Cells.Add(td);

		td.Controls.Add(RawImg(url, height));
		return td;
	}
	public Image RawImg(string url, int height = 50)
	{
		var img = new Image();
		img.Height = new Unit(height);
		img.ImageUrl = url;
		return img;
	}
}